//
//  ViewController.m
//  FMDBSingle
//
//  Created by qianfeng on 16/5/31.
//  Copyright © 2016年 qianfeng. All rights reserved.
//

#import "ViewController.h"
#import "AddViewController.h"
#import "DatabaseManager.h"
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,addDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     [self loadData];
    
    [self createTableView];
    [self createItems];
   
    
}

#pragma mark -- createTableView
- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cellID"];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
   // self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:_tableView];

}

- (void)createItems{
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemWasClick:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemWasClick:)];
    
    [rightItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)loadData{
    
 DatabaseManager *manager = [DatabaseManager sharedManager];
   NSArray *arr = [manager selectAllDatas];
//    for (Student *stu in arr) {
//        NSLog(@"%@",stu);
//    }
    [self.dataArray addObjectsFromArray:arr];
    NSLog(@"%lu",_dataArray.count);
    
    //[self createTableView];
    [_tableView reloadData];
    
}

#pragma mark -- tableview Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];

    Student *stu = _dataArray[indexPath.row];
    
    NSLog(@"%@",stu);
    [cell refreshCellWithModel:stu];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *arr = @[_dataArray[indexPath.row]];
        [[DatabaseManager sharedManager]deleteDataWithArray:arr];
    }
    [_dataArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

#pragma mark -- cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddViewController *add = [[AddViewController alloc]init];
    Student *stu = _dataArray[indexPath.row];
    add.student = stu;
    
    [self.navigationController pushViewController:add animated:YES];
    
    
}


- (void)leftItemWasClick:(UIBarButtonItem *)item{
    
    AddViewController *add = [[AddViewController alloc]init];
    
    add.delegate = self;
    
    
    
    [self.navigationController pushViewController:add animated:YES];
    
    
}

- (void)sendMessage:(NSString *)name andAge:(NSString *)age andSex:(NSString *)sex{
    
    Student *newStudent = [[Student alloc] init];
    newStudent.stuName = name;
    newStudent.stuAge = age;
    newStudent.stuSex = sex;
    
    [_dataArray addObject:newStudent];
    
    [_tableView reloadData];
}

- (void)rightItemWasClick:(UIBarButtonItem *)item{
    _tableView.editing = !_tableView.editing;
 
}

#pragma mark -- 懒加载
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
